package com.wsoft.system.controller;

import com.wsoft.query.job.*;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import com.wsoft.system.service.ITsJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 定时任务表 前端控制器
 *
 * @author z001
 * @since 2024年10月23日 08:16:34
 */
@RestController
@Tag(name = "任务调度")
@RequestMapping("/job")
public class TsJobController {

    @Resource
    private ITsJobService jobService;


    @Operation(summary = "新增job", description = "新增job")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid JobInsertQuery query) {
        boolean b = jobService.insertJob(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改job", description = "修改job")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid JobUpdateQuery query) {
        boolean b = jobService.updateJob(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "修改job状态", description = "修改job状态")
    @PostMapping("/updateStatus")
    public ResponseResult updateStatus(@RequestBody JobUpdateQuery query) throws Exception {
        boolean b = jobService.updateStatus(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "执行一次", description = "执行一次")
    @PostMapping("/execute")
    public ResponseResult execute(@RequestBody JobExecutorQuery query) throws Exception {
        boolean b = jobService.executeJobOne(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "查询job列表", description = "查询job列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody JobListQuery query) {
        return ResponseResult.success(ResponsePage.restPage(jobService.listJob(query)));
    }

    @Operation(summary = "详情页面", description = "详情页面")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(jobService.getJobInfo(id));
    }

    @Operation(summary = "获取cron后续执行的时间", description = "获取cron后续执行的时间")
    @PostMapping("/nextExecute")
    public ResponseResult nextExecute(@RequestBody JobNextExecutorQuery query) {
        return ResponseResult.success(jobService.nextExecute(query));
    }

    @Operation(summary = "获取quartz当前正在执行的定时任务", description = "获取quartz当前正在执行的定时任务")
    @GetMapping("/getExecutingJob")
    public ResponseResult getExecutingJobs() throws SchedulerException {
        return ResponseResult.success(jobService.getExecutingJobs());
    }


    @Operation(summary = "删除job", description = "删除job")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) throws Exception {
        boolean b = jobService.deleteJob(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

}
